requirejs.config({
    baseUrl: "js/",
    paths: {
        "jquery": ["/common/vendor/jquery-2.1.4/jquery-2.1.4.min"],
        "base": ["/common/scripts/base"],
        "ajax": ["/common/scripts/ajax"],
        "underscore": ["/common/vendor/underscore-1.8.3/underscore.min"]
    }
});
require(["jquery", "base", "ajax","underscore"], function($, base, ajax,_) {
    "use strict";

    var vipNews = function() {
        var _this = this;
        this.common = {
            //会员咨讯接口
            vipnewsInterface: {
                url: '/reqxml?action=27401&path=/info/vip_info_activity/info',
                data: {
                    clientNo: '($account)',
                    uid: '($weakaccount)',
                    wtoken: '($weaktoken)'
                }
            },
            weakAccount:"",
            testBtn: base.LOG_ON //是否是测试开关 
        };
        this.init();
        //返回刷新数据(第二天才会生效，不用刷新了)
        // window.GoBackOnLoad = function() {
        //     _this.init();
        // };
    };

    vipNews.prototype = {
        /**
         * [init 初始化]
         * @return {[type]} [description]
         */
        init: function() {
            var _this = this;
            this.getWeakAccount(function(){
                _this.getData();
            });
            $(".public-header .public-header-back").click(function(){
                base.href("http://action:3413");
            });
        },
        getWeakAccount: function(callback) {
        //获取弱账号
        var _this = this;
        var sUrl = '/reqlocal?jyloginflag=&weakaccount=';
            ajax.run(sUrl, '', function(oData) {
              if(oData.WEAKACCOUNT && oData.WEAKACCOUNT != "undefined"){
                _this.common.weakAccount = oData.WEAKACCOUNT;
              }
              callback && callback();
            });
        },
        /**
         * [getData 获取页面数据]
         * @return {[type]} [description]
         */
        getData: function(obj) {
            var _this = this;
            //获取会员咨讯数据
            TalkingData.onHTSCEvent("page","leaf426", "会员资讯专区."+_this.common.weakAccount, "", "");
            _this.getvipnews();
        },
        /**
         * [getvipnews 获取会员咨讯数据]
         * @return {[type]} [description]
         */
        getvipnews: function() {
            var _this = this;
            ajax.run(this.common.vipnewsInterface.url, this.common.vipnewsInterface.data, function(xml) {    
                var data = $.parseJSON(xml.BINDATA);
                if (data.code === "0") {
                    if (data.data && data.data.length) {
                        _this.renderInfoScreen(data.data);
                    } else {
                        if (_this.common.testBtn) {
                            base.log("数据列表取不出来");
                        }
                    }
                } else {
                    if (_this.common.testBtn && data.msg) {
                        alert(data.msg);
                    }
                }
            }, function(xml) {
                if (_this.common.testBtn && xml.ERRORMESSAGE) {
                    alert(xml.ERRORMESSAGE);
                }
            });
        },
        /**
         * [renderInfoScreen 接口页面渲染]
         * @return {[type]} [description]
         */
        renderInfoScreen: function(data) {
            $('.main').html(_.template($('#main').html())({
                data: data
            }));
            //页面绑定事件
            this.bindEvent();
        },
        /**
         * [bindEvent 页面事件绑定]
         * @return {[type]} [description]
         */
        bindEvent: function() {
            //四大咨讯产品链接跳转
            var _this = this;
            $(".li-list").off("click").on("click", function() {
                var $this = $(this);
                var prdt_id = $this.attr("data-prdt-id");
                var prdt_ishave = $this.attr("data-ishave");
                var prdt_title = $this.attr("data-title");
                if(prdt_ishave == "0"){
                    //未订阅
                    var url = 'http://action:10061/?fullscreen=1&&type=9&&url=/zlcftajax/sc/zx_xx_xqy.htm?prdt_id='+ prdt_id + '&proType=4&type=12';
                }else{
                    //已订阅
                    var url = "http://action:10061/?type=1&&fullscreen=1&&url=/zlcftajax/adds/zqyj.htm?nId=" + prdt_id + "&title=" + encodeURI(prdt_title);
                }
                //埋点
                var mdUnit = "";
                switch (prdt_id) {
                    case "SP0542":
                        mdUnit = 'zxyb';
                        break;
                    case "SP0539":
                        mdUnit = 'ztbjm'
                        break;
                    case "SP0540":
                        mdUnit = 'pqjh'
                        break;
                    case "SP0541":
                        mdUnit = 'yddp'
                        break;
                    default:
                        mdUnit = 'zxyb'
                }
                TalkingData.onHTSCEvent("click","leaf426", "会员资讯专区."+_this.common.weakAccount, mdUnit, "免费领取");
                base.log("url=" + url);
                if (url) {
                    base.href(url);
                }
                return;
            });
            //持仓股综合分析链接跳转
            $(".footer .one").off("click").on("click", function() {
                var url = "http://action:10090/?url=" + encodeURIComponent("http://action:10061/?shownavigationbar=1&&type=9&&topcolor=F64843&&fullscreen=1&&url=/zlcftajax/information/position/main.htm?source=&currency=&dateType=");
                TalkingData.onHTSCEvent("click","leaf426", "会员资讯专区."+_this.common.weakAccount, "", "持仓股综合分析");
                base.log("url=" + url);
                if (url) {
                    base.href(url);
                }
                return;
            });
            //形态相似K线链接跳转
            $(".footer .two").off("click").on("click", function() {
                TalkingData.onHTSCEvent("click","leaf426", "会员资讯专区."+_this.common.weakAccount, "", "形态相似K线");
                $.ajax({
                    url: "/reqlocal?upversion=",
                    type: "get",
                    success: function(data) {
                        var v = data.UPVERSION;
                        if (base.versionCompare(v, "4.0.4") >=0) {
                            base.href("http://action:82104")
                        }else{
                            base.href("http://action:10061/?fullscreen=1&&shownavigationbar=0&&url=/zlcftajax/ttf/xskx/index.htm")
                        }
                    }
                })
            });
        }
    };

    return new vipNews();
});